#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 时间     : 2018-09-03 11:13:58
# 作者     : DL (584054432@qq.com)
# 网址     : https://gitee.com/dingliangPython/
# 软件版本 : Python3.6.5
# 功能     ：time,模块补充

import time
import re
import logging
logger=logging.getLogger("main.time")

def str_to_utc(s):
    """摘要

    功能描述：字符串转UtC时间戳

    参数:
        s: [描述: 时间字符串,如:2018-11-12 12:12:12/ 18/2/1 1:3:4/ 20180102030405]

    返回:
        [返回值描述:UtC时间戳 ]
        [类型 int]
    """
    try:
        if re.findall('^\d+\D+\d+\D+\d+\D+\d+\D+\d+\D+\d+$', s):
            strTime = [
                '20'+v if k == 0 and len(v) == 2 else v for k, v in enumerate(re.findall('\d+', s))]
        elif len(s) == 14 and s.isdigit():
            strTime = [s[:4] if v == 0 else s[(
                v+1)*2:(v+1)*2+2] for v in range(6)]

        t = "{}-{:0>2s}-{:0>2s} {:0>2s}:{:0>2s}:{:0>2s}".format(*strTime)
        return int(time.mktime(time.strptime(t, '%Y-%m-%d %H:%M:%S')))
    except Exception as e:
        logger.error("日期格式不正确认:%s,需要格式:如:2018-11-12 12:12:12/ 18/2/1 1:3:4/ 20180102030405" % e)
        return False


def utc_to_datetime(s):
    """摘要

    功能描述：utc时间戳转本地格式时间

    参数:
        s: [描述: utc时间戳]

    返回:
        [返回值描述: 本地时间字符串]
        [类型 str]
    """
    try:
        if s and len(str(s)) >= 10:
            t = str(s)
            return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(t[:10])))
    except Exception as e:
        logger.error("正确认格式为：1970-1-1到现在的秒数，如：1535980488，日期格式不正确认:%s" % e)
        return False


def get_current_time():
    """摘要

    功能描述：返回当前日期时间

    返回:
        dict 类型 datetime,date,time,utc,year,mon,day,hour,min,sec,week
    """
    t = time.localtime(time.time())
    utc = int(time.time())
    datetime = time.strftime("%Y-%m-%d %H:%M:%S", t)
    date = time.strftime("%Y-%m-%d", time.localtime(time.time()))
    stime = time.strftime("%H:%M:%S", t)
    return {'datetime': datetime, 'date': date, 'time': stime, 'utc': utc, 'year': t.tm_year, 'mon': t.tm_mon, 'day': t.tm_mday,
            'hour': t.tm_hour, 'min': t.tm_min, 'sec': t.tm_sec, 'week': t.tm_wday+1}

now=get_current_time
def format_datetime(s):
    """摘要

    功能描述：格式化字符串时间,如:如:2018/11/12@12:12:12, 18/2/1 1:3:4, 20180102030405,2018/12/11 ]

    参数:
        s: [描述: 时间字符]

    返回:
        [返回值描述: 本地时间字符串]
        [类型]
    """
    try:
        if s and s.isdigit() and len(s) == 14:
            strTime = [s[:4] if v == 0 else s[(
                v+1)*2:(v+1)*2+2] for v in range(6)]
        elif re.findall('^\d+\D+\d+\D+\d+\D+\d+\D+\d+\D+\d+$', s):
            strTime = [
                '20'+v if k == 0 and len(v) == 2 else v for k, v in enumerate(re.findall('\d+', s))]
        elif len(s) == 10 and len(re.findall('\d+', s)[0]) == 4:
            ss = s+' 00:00:00'
            strTime = [
                '20'+v if k == 0 and len(v) == 2 else v for k, v in enumerate(re.findall('\d+', ss))]
        elif len(s) > 10:
            strTime = re.findall('\d+', s)
            if len(strTime) != 6:
                strTime += ['00' for v in range(6-len(strTime))]
        datetime = "{}-{:0>2s}-{:0>2s} {:0>2s}:{:0>2s}:{:0>2s}".format(
            *strTime)
        return datetime
    except Exception as e:
        logger.error("日期格式不正确认:%s" % e)
        return False


def unsigned_datatime(s):
    """摘要

    功能描述：无符号时间

    参数:
        s: [描述: 带符号字符]

    返回:
        [返回值描述: 无符号时间字符串 ]
        [类型 str]
    """
    try:
        strTime = re.findall('\d+', format_datetime(s))
        t = "{}{:0>2s}{:0>2s}{:0>2s}{:0>2s}{:0>2s}".format(*strTime)
        return t
    except Exception as e:
        logger.error("日期格式不正确认:%s,需要格式:如:2018-11-12 12:12:12/ 18/2/1 1:3:4/ 20180102030405" % e)


if __name__ == '__main__':
    logger.error(str_to_utc('2019、01 03 01 01 01'))

    logger.error(type(time))
